package com.yf.gogameserver.controller;/*
@author FAN

@create 2021-12-21-8:35

用户注册
*/

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.yf.gogameserver.entity.User;
import com.yf.gogameserver.service.IUserService;
import com.yf.gogameserver.utils.MapCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private DefaultKaptcha kaptcha;
    //暂存验证码集合
    private Map<String, String> codeMap = new HashMap<String, String>();

    @RequestMapping("/register")
    public Integer register(User user) {
        System.out.println(user);
        int result = userService.register(user);


        return result;
    }

    //登录
    @RequestMapping("/login")
    public User login(String username, String password, String code, String codeKey) {
        System.out.println("接收到的请求：" + username + "---" + password + "---" + code + "---");
        //获取服务生成的验证码
        String serverCode = codeMap.get(codeKey);
        if (!code.equals(serverCode)) {
            User user = new User();
            user.setUsername("-1");
            return user;
        }
        //匹配正常就删除codeKey
        codeMap.remove(codeKey);


        //判断用户登录状态
        User user = userService.login(username, password);


        return user;//返回客户端
    }

    //生成验证码
    @RequestMapping("/code")
    public void createCode(HttpServletResponse response, String codeKey) throws IOException {
        //生成验证码
        String code = kaptcha.createText();
        //保存到map集合中
        codeMap.put(codeKey, code);
        //生成图片
        BufferedImage image = kaptcha.createImage(code);
        //直接将图片通过response响应客户端
        ImageIO.write(image, "jpg", response.getOutputStream());
    }

    /**
     * 通过账号给邮箱发送验证码，找回密码
     *
     * @return
     */
    @RequestMapping("/sendEmailCode")
    public int sendEmailCode(String username) {

        int result = userService.sendEmailByUserName(username);

        return result;
    }

    @RequestMapping("/updatePassword")
    public int updatePassword(String username, String newpassword, String code) {
        //校验验证码,用户输入的验证码
        //获取原有发邮件验证码
        String oldCode = MapCodeUtils.getCode(username);
        if (oldCode == null) {
            //无效验证码获已过期
            return -1;
        }
        //比较验证码
        if (!oldCode.equals(code)) {
            //验证码有误
            return -2;
        }
        //验证码通过
        //调用service层改密码

        int result = userService.updatePassword(username, newpassword);


        //将验证码删除
        MapCodeUtils.removeCode(username);

        return result;
    }
}
